package com.wgh.product.service.impl;

import com.wgh.product.entity.shop.ProductSkuAttribute;
import com.wgh.product.mapper.ProductSkuAttributeMapper;
import com.wgh.product.service.ProductSkuAttributeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * SKU属性关联服务实现类
 * @author AI Shop
 */
@Service
public class ProductSkuAttributeServiceImpl extends ServiceImpl<ProductSkuAttributeMapper, ProductSkuAttribute> implements ProductSkuAttributeService {

    @Override
    public List<ProductSkuAttribute> getSkuAttributesBySkuId(Long skuId) {
        QueryWrapper<ProductSkuAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id", skuId)
                   .eq("del_flag", 0);
        return list(queryWrapper);
    }

    @Override
    public List<ProductSkuAttribute> getSkuAttributesBySkuIds(List<Long> skuIds) {
        QueryWrapper<ProductSkuAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("sku_id", skuIds)
                   .eq("del_flag", 0);
        return list(queryWrapper);
    }

    @Override
    public List<ProductSkuAttribute> getSkuAttributesByAttributeId(Long attributeId) {
        QueryWrapper<ProductSkuAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attribute_id", attributeId)
                   .eq("del_flag", 0);
        return list(queryWrapper);
    }

    @Override
    public List<ProductSkuAttribute> getSkuAttributesByAttributeValueId(Long attributeValueId) {
        QueryWrapper<ProductSkuAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attribute_value_id", attributeValueId)
                   .eq("del_flag", 0);
        return list(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchAddSkuAttributes(List<ProductSkuAttribute> skuAttributes) {
        for (ProductSkuAttribute skuAttribute : skuAttributes) {
            skuAttribute.setDelFlag(0);
        }
        return saveBatch(skuAttributes);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteSkuAttributesBySkuId(Long skuId) {
        QueryWrapper<ProductSkuAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id", skuId);
        return remove(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchDeleteSkuAttributesBySkuIds(List<Long> skuIds) {
        QueryWrapper<ProductSkuAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("sku_id", skuIds);
        return remove(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteSkuAttributesByAttributeId(Long attributeId) {
        QueryWrapper<ProductSkuAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attribute_id", attributeId);
        return remove(queryWrapper);
    }

    @Override
    public List<ProductSkuAttribute> getSkuAttributesByProductId(Long productId) {
        return baseMapper.selectByProductId(productId);
    }
}